﻿// ———————————————————————–
// <copyright file="Incident.cs" company="The MITRE Corporation">
//    Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// Incident.cs:
//
// Represents a CAD Incident
//
// Project: IC.NET - CAD Library
// Author:  Jim Cronin - MITRE
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)

using System;
using System.Xml;

namespace EDXLSharp.NIEMCADLib
{
  /// <summary>
  /// Represents a CAD Incident
  /// </summary>
  public class IncidentDetail : Detail
  {
    /// <summary>
    /// Status designation for the incident. Structure is TBD
    /// </summary>
    public IncidentStatus Status { get; set; }

    /// <summary>
    /// Organization responsible for responding to the incident
    /// </summary>
    public IncidentOwningOrganization OwningOrganization { get; set; }

    /// <summary>
    /// Alternate method of describing the incident's location
    /// </summary>
    public IncidentLocationExtension LocationExtension { get; set; }
    
    /// <summary>
    /// Instantiates a new Incident instance
    /// </summary>
    public IncidentDetail()
    {
    }

    public override string ElementName
    {
      get { return "IncidentDetail"; }
    }

    public override string ElementNamespace
    {
      get { return EDXLConstants.CADNamespace; }
    }

    public override string ElementPrefix
    {
      get { return EDXLConstants.CADPrefix; }
    }

    /// <summary>
    /// Initializes a new instance of the Incident class from a string
    /// </summary>
    /// <param name="xmlString">Valid XML Incident data in a string</param>
    /// <exception cref="System.ArgumentException">Thrown if xmlString is null, empty, whitespace, or invalid XML</exception>
    public IncidentDetail(string xmlString) : this()
    {
      if (string.IsNullOrWhiteSpace(xmlString))
      {
        throw new ArgumentNullException("xmlString", "XML must not be null or blank");
      }

      XmlDocument doc = new XmlDocument();
      try
      {
        doc.LoadXml(xmlString);
      }
      catch (XmlException xe)
      {
        throw new ArgumentException("Error parsing XML. See inner exception for details", xe);
      }

      this.ReadXML(doc.DocumentElement);
    }

    /// <summary>
    /// Writes this Object to an inline XML Document
    /// </summary>
    /// <param name="xwriter">Valid XMLWriter</param>
    public override void WriteXML(XmlWriter xwriter)
    {
      if (xwriter == null)
      {
        throw new ArgumentNullException("xwriter");
      }

      this.Validate();

      xwriter.WriteStartElement(ElementPrefix, ElementName, ElementNamespace);

      if (this.Status != null)
      {
        this.Status.WriteXML(xwriter);
      }
      
      if (this.OwningOrganization != null)
      {
        this.OwningOrganization.WriteXML(xwriter);
      }

      if (this.LocationExtension != null)
      {
        this.LocationExtension.WriteXML(xwriter);
      }

      xwriter.WriteEndElement();
    }

    /// <summary>
    /// Reads this Object's values from an XML NodeList
    /// </summary>
    /// <param name="rootnode">Root XML Node of the Object Element</param>
    public override void ReadXML(XmlNode rootnode)
    {
      if (rootnode == null)
      {
        throw new ArgumentNullException("rootnode");
      }
      if (rootnode.LocalName != this.ElementName)
      {
        throw new ArgumentException("Unexpected element " + rootnode.LocalName + " instead of " + this.ElementName);
      }

      foreach (XmlNode node in rootnode.ChildNodes)
      {
        switch (node.LocalName)
        {
          case "IncidentStatus":
            if (this.Status != null)
            {
              throw new ArgumentException("There should only be a single incident status.");
            }
            this.Status = new IncidentStatus(node.OuterXml);
            break;
          case "IncidentOwningOrganization":
            if (this.OwningOrganization != null)
            {
              throw new ArgumentException("There should only be a single incident owning organization.");
            }
            this.OwningOrganization = new IncidentOwningOrganization();
            this.OwningOrganization.ReadXML(node);
            break;
          case "IncidentLocationExtension":
            if (this.LocationExtension != null)
            {
              throw new ArgumentException("There should only be a single incident location extension.");
            }
            this.LocationExtension = new IncidentLocationExtension(node.OuterXml);
            break;
          default:
            throw new ArgumentException("Incident.ReadXML - unrecognized child node not implemented");
        }
      }

      this.Validate();
    }

    /// <summary>
    /// Checks content for validity, and throws an exception if there are issues
    /// </summary>
    /// <exception cref="EDXLSharp.NIEMCADLib.ValidationException">Thrown if validation fails</exception>
    public override void Validate()
    {
      if(this.Status == null)
      {
        throw new ValidationException("Incident Status is missing");
      }
      this.Status.Validate();

      if (this.OwningOrganization == null)
      {
        throw new ValidationException("Incident Owning Organization is missing");
      }
      this.OwningOrganization.Validate();
      
      if (this.LocationExtension != null)
      {
        this.LocationExtension.Validate();
      }
    }
  }
}
